# cython: language_level=3
import os
import sentry_sdk
from sentry_sdk.integrations.sanic import SanicIntegration
from .base import *
from .base import APPLICATIONS
from dotenv import load_dotenv

# 加载.env文件
load_dotenv(verbose=True)

# 框架配置
DEBUG = os.getenv("DEBUG", True)
ACCESS_LOG = os.getenv("ACCESS_LOG", True)
AUTO_RELOAD = os.getenv("AUTO_RELOAD", True)

# 数据库 配置
DB_DRIVER=os.getenv("DB_DRIVER", "mysql")
DB_HOST=os.getenv("DB_HOST", "localhost")
DB_PORT=os.getenv("DB_PORT", "3306")
DB_DATABASE=os.getenv("DB_DATABASE", "sanic")
DB_USERNAME=os.getenv("DB_USERNAME", "root")
DB_PASSWORD=os.getenv("DB_PASSWORD", "123456")

DB_CHARSET=os.getenv("DB_CHARSET", "utf8mb4")
DB_COLLATION=os.getenv("DB_COLLATION", "utf8mb4_unicode_ci")
DB_PREFIX=os.getenv("DB_PREFIX")
DB_TRUST_SERVER_CERTIFICATE=os.getenv("DB_TRUST_SERVER_CERTIFICATE", 'true')
DB_ENCRYPT=os.getenv("DB_TRUST_SERVER_CERTIFICATE", 'yes')

DB_URL = DB_DRIVER + "://" + DB_USERNAME + ":" + DB_PASSWORD + "@" + DB_HOST + ":" + DB_PORT + "/" + DB_DATABASE + "?charset=" + DB_CHARSET
# DB_URL = os.getenv("DB_URL", "mysql://root:123456@localhost:3306/sanic?charset=utf8mb4")
#DB_URL = os.getenv("DB_URL", "mysql://phdcdb:JdhgeU3723%^@192.168.0.129:3306/sanic?charset=utf8mb4")

# redis 配置
REDIS_HOST=os.getenv("REDIS_HOST", "localhost")
REDIS_PORT=os.getenv("REDIS_PORT", 6379)
REDIS_PASSWORD=os.getenv("REDIS_PASSWORD")
REDIS_DB=os.getenv("REDIS_DB")
REDIS_PREFIX=os.getenv("REDIS_PREFIX")
REDIS_URL = "redis://" + REDIS_HOST + ":" + REDIS_PORT

TORTOISE_ORM = {
    "connections": {"default": DB_URL},
    "apps": {
        "models": {
            "models": ["aerich.models"]
            + [app + ".models" for app in APPLICATIONS],  # noqa
            "default_connection": "default",
        },
    },
}

# start sentry
dsn=os.getenv("SENTRY_DSN", "http://*****")
environment=os.getenv("ENVIRONMENT", "dev")
def validate_string(string):
    # 检查字符串是否以"https://"开始
    if not string.startswith("https://"):
        return False
    # 按照@符号分割字符串
    parts = string.split("@")
    if len(parts) != 2:
        return False
    # 检查第二部分是否以.ingest.sentry.io/开始
    if not parts[1].startswith(".ingest.sentry.io/"):
        return False
    # 检查第一部分是否包含必要的信息
    credentials = parts[0].split(":")
    if len(credentials) != 2 or credentials[0] == "" or credentials[1] == "":
        return False
    return True

if validate_string(dsn):
    # print("字符串符合规范")
    sentry_sdk.init(
        dsn=dsn,
        integrations=[SanicIntegration()],
        environment=environment,
        # send_default_pii=True,
        
        # Set traces_sample_rate to 1.0 to capture 100%
        # of transactions for performance monitoring.
        # We recommend adjusting this value in production.
        traces_sample_rate=1.0,
        # Set profiles_sample_rate to 1.0 to profile 100%
        # of sampled transactions.
        # We recommend adjusting this value in production.
        # profiles_sample_rate=1.0,
    )
else:
    print("sentry配置不符合规范")